﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL.DCollector;
using System.Collections.Generic;

namespace BOL
{
    public class CategoryManager
    {
        public static CategoryManager Instance = new CategoryManager();

        public string getMenuCategory(string table, int catid1)
        {
            string menu = "";
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectByColumns(table, " * ", "selectbycolumns");
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Category_ID1=" + catid1;
            DataTable temp = dv.ToTable();
            menu += "<ul class=\"nav-sidebox\">";
            for (int i = 0; i < temp.Rows.Count; i++)
            {
                menu += getChild1MenuCategoryGroup(dt, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));
            }
            menu += "</ul>";
            return menu;

        }

        public string getMenuCategory(string table)
        {
            string menu = "";
            bool check;
            DataTable dt = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_Category ORDER BY Category_Ordinal ASC");
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Category_ID2=0 AND Category_ActiveLeft=1";
            DataTable temp = dv.ToTable();
            menu += "<ul class=\"nav-menu\">";
            for (int i = 0; i < temp.Rows.Count; i++)
            {

                check = checkChild(dt, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));


                if (check == true)
                {

                    menu += "<li><a  href=\"/Danhmuc" + temp.Rows[i]["Category_ID1"].ToString() + "/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/danhmuc.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";

                    menu += getChild1MenuCategory(dt, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));

                    menu += "</li>";
                }
                else
                {
                    menu += "<li><a  href=\"/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/" + temp.Rows[i]["Category_ID1"].ToString() + "/danhsachsanpham.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";

                    menu += getChild1MenuCategory(dt, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));

                    menu += "</li>";
                }
            }
            menu += "</ul>";
            return menu;

        }

        public string getMenuTopCategory(string table)
        {
            string menu = "";
            bool check;
            DataTable dt = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_Category Where Category_ID2=0 AND Category_ActiveTop=1 ORDER BY Category_Ordinal ASC");
            DataView dv = dt.DefaultView;
            dv.RowFilter = "Category_ID2=0 AND Category_ActiveTop=1 ";
            DataTable temp = dv.ToTable();
            for (int i = 0; i < temp.Rows.Count; i++)
            {

                check = checkChild(dt, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));


                if (check == true)
                {


                    menu += "<li><a href=\"/Danhmuc" + temp.Rows[i]["Category_ID1"].ToString() + "/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/danhmuc.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";

                    menu += "</li>";
                }
                else
                {
                    menu += "<li><a href=\"/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/" + temp.Rows[i]["Category_ID1"].ToString() + "/danhsachsanpham.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";


                    menu += "</li>";
                }
            }
            menu += "</ul>";
            return menu;

        }


        public string getChild1MenuCategoryGroup(DataTable table, int parent_id)
        {
            string childMenu = "";
            bool check;
            // Kiem tra co con hay ko
            DataView dv = table.DefaultView;
            dv.RowFilter = "Category_ID2=" + parent_id;
            DataTable temp = dv.ToTable();
            if (temp.Rows.Count > 0)
            {

                for (int i = 0; i < temp.Rows.Count; i++)
                {

                    check = checkChild(table, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));


                    if (check == true)
                    {

                        childMenu += " <li class=\"nav-sidebox-lv0\"><h3><a  href=\"/Danhmuc" + temp.Rows[i]["Category_ID1"].ToString() + "/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/danhmuc.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a></h3>";

                        childMenu += getChild2MenuCategoryGroup(table, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));

                        childMenu += "</li>";
                    }
                    else
                    {
                        childMenu += " <li class=\"nav-sidebox-lv0\"><h3><a href=\"/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/" + temp.Rows[i]["Category_ID1"].ToString() + "/danhsachsanpham.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a></h3>";

                        childMenu += getChild2MenuCategoryGroup(table, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));

                        childMenu += "</li>";
                    }

                }



            }

            return childMenu;

        }

        public string getChild1MenuCategory(DataTable table, int parent_id)
        {
            string childMenu = "";
            bool check;
            // Kiem tra co con hay ko
            DataView dv = table.DefaultView;
            dv.RowFilter = "Category_ID2=" + parent_id;
            DataTable temp = dv.ToTable();
            if (temp.Rows.Count > 0)
            {
                childMenu += "<ul class=\"nav-1\">";

                for (int i = 0; i < temp.Rows.Count; i++)
                {

                    check = checkChild(table, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));


                    if (check == true)
                    {

                        childMenu += " <li class=\"lv2\"><a href=\"/Danhmuc" + temp.Rows[i]["Category_ID1"].ToString() + "/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/danhmuc.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";

                        childMenu += getChild2MenuCategory(table, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));

                        childMenu += "</li>";
                    }
                    else
                    {
                        childMenu += " <li class=\"lv2\"><a href=\"/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/" + temp.Rows[i]["Category_ID1"].ToString() + "/danhsachsanpham.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";

                        childMenu += getChild2MenuCategory(table, Convert.ToInt32(temp.Rows[i]["Category_ID1"].ToString()));

                        childMenu += "</li>";
                    }

                }

                childMenu += "</ul>";

            }

            return childMenu;

        }

        public bool checkChild(DataTable table, int parent_id)
        {
            DataView dv = table.DefaultView;
            dv.RowFilter = "Category_ID2=" + parent_id;
            DataTable temp = dv.ToTable();
            if (temp.Rows.Count > 0) return true;

            return false;
        }

        public string getFather(DataTable table, int son_id)
        {
            DataView dv = table.DefaultView;
            dv.RowFilter = "Category_ID1=" + son_id;
            DataTable temp = dv.ToTable();
            return temp.Rows[0]["Category_ID2"].ToString();
        }

        public string getChild2MenuCategory(DataTable table, int parent_id)
        {
            string childMenu = "";
            // Kiem tra co con hay ko
            DataView dv = table.DefaultView;
            dv.RowFilter = "Category_ID2=" + parent_id;
            DataTable temp = dv.ToTable();
            if (temp.Rows.Count > 0)
            {
                childMenu += "<ul class=\"nav-2\">";

                for (int i = 0; i < temp.Rows.Count; i++)
                {

                    childMenu += " <li class=\"lv3\"><a href=\"/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/" + temp.Rows[i]["Category_ID1"].ToString() + "/danhsachsanpham.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";
                    childMenu += "</li>";
                }

                childMenu += "</ul>";
            }

            return childMenu;
        }
        public string getChild2MenuCategoryGroup(DataTable table, int parent_id)
        {
            string childMenu = "";
            // Kiem tra co con hay ko
            DataView dv = table.DefaultView;
            dv.RowFilter = "Category_ID2=" + parent_id;
            DataTable temp = dv.ToTable();
            if (temp.Rows.Count > 0)
            {
                childMenu += "<ul class=\"sidebox-0\">";

                for (int i = 0; i < temp.Rows.Count; i++)
                {

                    childMenu += " <li class=\"nav-sidebox-lv1\"><a href=\"/" + LIB.Converter.convertLink(temp.Rows[i]["Category_Name"].ToString()) + "/" + temp.Rows[i]["Category_ID1"].ToString() + "/danhsachsanpham.htm\">" + temp.Rows[i]["Category_Name"].ToString() + "</a>";
                    childMenu += "</li>";
                }

                childMenu += "</ul>";
            }

            return childMenu;
        }
    }
}
